<?php

foreach(Salle::loadAll() as $salle){
$selected='';
if(isset($_POST['SALL_NUMID']) && $_POST['SALL_NUMID']==$salle->getId()) $selected=' selected';
?>
<form method="post" class="js_form_salle js_form_salle_<?= $salle->SALL_NUMID.$selected ?>">
	<input type="hidden" value="<?= $salle->SALL_NUMID ?>" class="js_id_salle" name="SALL_NUMID"/>
	<input type="hidden" value="<?= $salle ?>" class="js_nom_salle"/>
	<p>
		<label>
			Film :
			<select name="FILM_NUMID" class="js_id_film">
	<?php foreach(Film::loadAll() as $film){ ?>
	<?php
	$selected='';
	if(isset($_POST['FILM_NUMID']) && $_POST['FILM_NUMID']==$film->getId()) $selected=' selected';
	?>
				<option value="<?= $film->getId() ?>"<?= $selected ?>>
					<?= $film ?> (durée : <?= Date::formaterDuree($film->FILM_DUREE) ?>)
				</option>
	<?php } ?>
			</select>
		</label>
		<label>
			Type de séance :
			<select name="TYPE_NUMID" class="js_id_seance">
	<?php foreach(TypeSeance::loadAll() as $seance){ ?>
	<?php
	$selected='';
	if(isset($_POST['TYPE_NUMID']) && $_POST['TYPE_NUMID']==$seance->getId()) $selected=' selected';
	?>
				<option value="<?= $seance->getId() ?>"<?= $selected ?>>
					<?= $seance ?>
				</option>
	<?php } ?>
			</select>
		</label>
		<span><?= $salle->SALL_LIBEL ?></span>
	</p>
	<table class="programme" border="1">
		<thead>
			<tr>
				<th></th>
	<?php
	foreach($programmation['jours'] as $jour => $progJour){
		$cssToday=$progJour['today']?'class="today"':'';
		$label=$progJour['label'];
		$court=$progJour['court'];
		echo("<td $cssToday>$court<div style='display:none'>$label</div></td>".PHP_EOL);
	}
	?>
			</tr>
		</thead>	
		<tbody>
			<tr>
				
	<?php
	foreach($programmation['horaires']['heures'] as $horaire){
		echo('<th>'.$horaire.'</th>');
		break;
	}
	foreach($programmation['jours'] as $jour => $progJour){
		$cssOuvert=$progJour['ferme']?' class="ferme"':' class="ouvert"';
		$nbSegments=count($programmation['horaires']['heures']);
		echo("<td rowspan='$nbSegments'$cssOuvert>");
		if(!$progJour['ferme']){
			foreach($progJour['creneaux'] as $creneau){
				$crenId=$creneau['id'];
				$height=76*count($programmation['horaires']['heures']); //2h = 76px
				$top=$creneau['top']*$height/100;
				$heure=Date::formaterDuree($creneau['heure']);
				$progDuCreneauEtDeLaSalle=null;
				foreach($creneau['progs'] as $progDuCreneau){
					if($progDuCreneau->SALL_NUMID==$salle->SALL_NUMID){
						$progDuCreneauEtDeLaSalle=$progDuCreneau;
						break;
					}
				}
				if($progDuCreneauEtDeLaSalle!=null){
					$film=Film::load($progDuCreneauEtDeLaSalle->FILM_NUMID);
					$height=$film->FILM_DUREE*38/60-4; //1h = 38px ; 4 pour les bordures
					$progId=$progDuCreneauEtDeLaSalle->PROG_NUMID;
					$fin=Date::formaterDuree(
						$creneau['heure']+$film->FILM_DUREE);
?>
				<span style="height: <?= $height ?>px;">
				<div class="creneau" style="top: <?= $top ?>px">
					<hr/>
					<span><?= $heure ?>-<?= $fin ?></span>
					<div class="nowrap">
						<button class="suppr" name="PROG_NUMID" value="<?= $progId ?>">x</button>
						<?= $film ?>
					</div>
					<hr/>
				</div>
				</span>
<?php } else{ ?>
				<div class="creneau" style="top: <?= $top ?>px;">
					<hr/>
					<span><?= $heure ?></span>
					<div class="nowrap">
<button action='ajouter' name='CREN_NUMID' value='<?= $crenId ?>' class='fakeLink'>Ajouter</button>
					</div>
					<hr/>
				</div>
<?php
				} 	
			}
		}
		echo('</td>');
	}
	?>
			</tr>
	<?php
	$premiere=true;
	foreach($programmation['horaires']['heures'] as $horaire){
		//la première est déjà affichée sur la première ligne
		if($premiere) $premiere=false;
		else echo('<tr><th>'.$horaire.'</th></tr>');
	}
	?>
		</tbody>
	</table>
</form>
<?php } ?>
